﻿using LiveChartsCore.Defaults;
using LiveChartsCore.Kernel.Sketches;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using MonitorPrism_Practice.Mvvm;
using MonitorPrism_Practice.Services;
using Newtonsoft.Json.Linq;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using static log4net.Appender.ColoredConsoleAppender;

namespace MonitorPrism_Practice.ViewModels
{
    public class InterfaceLineConfigViewModel : BindableBase, IViewLoadedAndUnloadedAware
    {
        private System.Windows.Media.Color _selectedColor = System.Windows.Media.Colors.Red; // 默认颜色
        TextBlock txt;
        UserControl window;
        // 可绑定的颜色属性
        public System.Windows.Media.Color SelectedColor
        {
            get => _selectedColor;
            set
            {
                SetProperty(ref _selectedColor, value);
                ChartsLine.List_LineSeries[LineIndex].Stroke = new SolidColorPaint()
                {
                    Color = SKColor.Parse(_selectedColor.ToString()),
                    StrokeThickness = ChartsLine.List_LineSeries[LineIndex].Stroke.StrokeThickness,
                };
               
            }
        }
        private bool _isColor;
        public bool IsColor
        {
            get => _isColor;
            set
            {
                    
                SetProperty(ref _isColor, value);
                if (value)
                    ChartsLine.List_LineSeries[LineIndex].Fill = new SolidColorPaint(
                       new SKColor(SelectedColor.R, SelectedColor.G, SelectedColor.B, 51));
                else ChartsLine.List_LineSeries[LineIndex].Fill = null;
  
            }
        }
        private LineSeries<ObservablePoint> _lineThicness ;
        public LineSeries<ObservablePoint> LineThickness
        {
            get
            {
                return _lineThicness;
            }
            set
            {
                SetProperty(ref _lineThicness, value);
            }
        }

        private int _lineIndex = 0;
        public int LineIndex
        {
            get => _lineIndex; set
            {
                if (value <0) return;
                LineThickness = ChartsLine.List_LineSeries[value];
                SetProperty(ref _lineIndex, value);
            }
        }
        private string _selectedName;
        public string SelectedName
        {
            get => _selectedName;
            set { SetProperty(ref _selectedName,value); }
        }

        private ObservableCollection<string> setThickness = new ObservableCollection<string>()
        {
            "1","1.5","2","2.5","3","3.5"
        }
            ;
        public ObservableCollection<string> SetThickness
        {
            get => setThickness; set
            {
                SetProperty(ref setThickness, value);
            }
        }

        private int thicknessSelected;
        public int ThicknessSelected
        {
            get => thicknessSelected; set
            {
                if (SetProperty(ref thicknessSelected, value))
                {
                    ChartsLine.LineIndex = value;

                }

            }
        }
        private ClassLiveCharts2Line _chartsLine;
        public ClassLiveCharts2Line ChartsLine { get => _chartsLine; set { SetProperty(ref _chartsLine, value); } }

        private ICommand _settingThicknessCommand;
        public ICommand SettingThicknessCommand
        {
            get
            {
                return this._settingThicknessCommand ?? (this._settingThicknessCommand = new DelegateCommand(() =>
                {
                    ChartsLine.List_LineSeries[LineIndex].Stroke.StrokeThickness =
                    float.Parse(SetThickness[ThicknessSelected]);
                    txt.Text = float.Parse(SetThickness[ThicknessSelected]).ToString();
                }));
            }
        }

        private ICommand _removeAllLineCommand;
        public ICommand RemoveAllLineCommand
        {
            get
            {
                return this._removeAllLineCommand ?? (this._removeAllLineCommand = new DelegateCommand(() => {
                    ChartsLine.LineName = null;
                    foreach (var item in ChartsLine.List_LineSeries)
                    {
                        item.Name = "空" + SelectedName;
                    }
                     }));

            }
        }

        public InterfaceLineConfigViewModel(ClassLiveCharts2Line classLiveCharts2Line)
        {
            ChartsLine = classLiveCharts2Line;
        }
      
        public void OnLoaded(object sender, RoutedEventArgs e)
        {
            window = sender as UserControl;
            txt = window.FindName("txt_thickness") as TextBlock;

        }

        public void OnUnloaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
